﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;


namespace Database
{
   public class LoaiMayTinh
    {
        private int maloai;

        public int Maloai
        {
            get { return maloai; }
            set { maloai = value; }
        }
        private string tenloai;

        public string Tenloai
        {
            get { return tenloai; }
            set { tenloai = value; }
        }
        public LoaiMayTinh()
        {
        }
        public LoaiMayTinh(int maloai, string tenloai)
        {
            this.maloai = maloai;
            this.tenloai = tenloai;
        }
        public static DataSet DSMaLoaiMT()
        {
            DataSet ds = new DataSet();
            string query = "select * from LoaiMayTinh";
            sqlConnection db = new sqlConnection();
            SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
            sda.Fill(ds);
            return ds;
        }

        public DataSet DSLoaiMayTinh()
        {
            DataSet ds = new DataSet();
            string query = "select * from LoaiMayTinh";
            sqlConnection db = new sqlConnection();
            SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
            sda.Fill(ds);
            return ds;
        }
        public bool ThemLoaiMT(string tenloai)
        {
            bool kq = true;

            try
            {
                string query = "insert into LoaiMayTinh(tenloai) values('{0}')";
                query = String.Format(query,tenloai);
                sqlConnection db = new sqlConnection();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                return cmd.ExecuteNonQuery() > 0;
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public bool XoaLoaiMayTinh(int maloai)
        {
            bool kq = true;

            try
            {
                string query = "Delete loaimaytinh where maloai='{0}'";
                query = String.Format(query,maloai);
                sqlConnection db = new sqlConnection();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                cmd.ExecuteNonQuery();
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public bool CapNhatLoaiMT(int maloai,string tenloai)
        {
            bool kq = true;

            try
            {
                string query = "update LoaiMayTinh set tenloai='{0}' where maloai={1}";
                query = String.Format(query,tenloai,maloai);
                sqlConnection db = new sqlConnection();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                return cmd.ExecuteNonQuery() > 0;
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public LoaiMayTinh ThongTinLoaiMT(int maloai)
        {
            string query = "select * from loaimaytinh where maloai=" + maloai;
            sqlConnection db = new sqlConnection();
            SqlCommand sc = new SqlCommand(query, db.GetConnect());
            SqlDataReader reader = sc.ExecuteReader();
            LoaiMayTinh lmt = new LoaiMayTinh();
            while (reader.Read())
            {
                lmt.maloai= reader.GetInt32(0);
                lmt.tenloai= reader.GetString(1);                
            }
            return lmt;
        }
    }
}
